<template>
  <div 
    class="bx-guide"
    v-if="visible"
  >
    <div class="bx-guide__overlay" @click="handleOverlayClick"></div>
    <div class="bx-guide__content bx-glass">
      <div class="bx-guide__header">
        <div class="bx-guide__title">{{ title }}</div>
        <BxIcon name="x" class="bx-guide__close" @click="handleClose" />
      </div>
      <div class="bx-guide__body">
        <slot>{{ description }}</slot>
      </div>
      <div class="bx-guide__footer">
        <BxButton v-if="currentStep > 0" size="small" @click="prevStep">上一步</BxButton>
        <BxButton size="small" theme="primary" @click="nextStep">{{ isLastStep ? '完成' : '下一步' }}</BxButton>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import BxButton from "../basic/BxButton.vue"
import BxIcon from "../basic/BxIcon.vue"

export default {
  name: 'BxGuide',
  components: {
    BxButton,
    BxIcon
  },
  props: {
    steps: {
      type: Array,
      default: () => []
    },
    modelValue: {
      type: Boolean,
      default: false
    },
    maskClosable: {
      type: Boolean,
      default: true
    }
  },
  emits: ['update:modelValue', 'finish', 'change'],
  setup(props, { emit }) {
    const visible = ref(props.modelValue)
    const currentStep = ref(0)

    const currentStepData = computed(() => props.steps[currentStep.value])
    const title = computed(() => currentStepData.value?.title || '')
    const description = computed(() => currentStepData.value?.description || '')
    const isLastStep = computed(() => currentStep.value === props.steps.length - 1)

    watch(() => props.modelValue, (newVal) => {
      visible.value = newVal
      if (newVal) {
        currentStep.value = 0
      }
    })

    const handleClose = () => {
      visible.value = false
      emit('update:modelValue', false)
    }

    const handleOverlayClick = () => {
      if (props.maskClosable) {
        handleClose()
      }
    }

    const prevStep = () => {
      if (currentStep.value > 0) {
        currentStep.value--
        emit('change', currentStep.value)
      }
    }

    const nextStep = () => {
      if (isLastStep.value) {
        emit('finish')
        handleClose()
      } else {
        currentStep.value++
        emit('change', currentStep.value)
      }
    }

    return {
      visible,
      currentStep,
      title,
      description,
      isLastStep,
      handleClose,
      handleOverlayClick,
      prevStep,
      nextStep
    }
  }
}
</script>

<style scoped>
.bx-guide {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
}

.bx-guide__overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
}

.bx-guide__content {
  position: relative;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-large);
  padding: 24px;
  width: 400px;
  max-width: 90%;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  font-family: var(--bx-font-family);
  color: var(--bx-text-primary);
}

.bx-guide__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.bx-guide__title {
  font-size: 20px;
  font-weight: 600;
}

.bx-guide__close {
  font-size: 18px;
  color: var(--bx-text-tertiary);
  cursor: pointer;
}

.bx-guide__close:hover {
  color: var(--bx-text-primary);
}

.bx-guide__body {
  font-size: 14px;
  line-height: 1.6;
  color: var(--bx-text-secondary);
  margin-bottom: 24px;
}

.bx-guide__footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* Responsive */
@media (max-width: 768px) {
  .bx-guide__content {
    padding: 16px;
  }
  .bx-guide__title {
    font-size: 18px;
  }
  .bx-guide__body {
    font-size: 13px;
  }
}
</style>

